package com.daotingli.backendprograms.Repository.Admin.Tag;

import com.daotingli.backendprograms.Bean.Bean.book_typeBean;
import com.daotingli.backendprograms.DB.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
@Repository
public class TagRepository {

    @Autowired
    private SpringDB springDB;
    @Autowired
    private book_typeBean book_type;

    public List<book_typeBean> getAllTag() {
        String sql = "SELECT * FROM book_type";
        List<book_typeBean> book_types = new ArrayList<>(); // 修复：初始化为空列表

        try {
            springDB.getJdbcTemplate().query(sql, (rs, rowNum) -> {
                book_type = new book_typeBean(rs.getInt("id"), rs.getString("type"));
                book_types.add(book_type);
                return book_type;
            });
        } catch (DataAccessException e) {
            throw new RuntimeException(e);
        }
        return book_types;
    }




    public Map<String, Object> addTag(String type) {
        Map<String, Object> response = new HashMap<>();
        String sql = "INSERT INTO book_type (type) VALUES (?)";

        try {
            springDB.getJdbcTemplate().update(sql, type);
            response.put("status", "success");
            response.put("message", "标签添加成功");
        } catch (Exception e) {
            response.put("status", "error");
            response.put("message", "标签添加失败: " + e.getMessage());
        }

        return response;
    }

    public Map<String, Object> deleteTag(Integer id) {
        Map<String, Object> response = new HashMap<>();
        String deleteViewSQL = "DELETE FROM book_type_view WHERE booktypeid = ?";
        String deleteTagSQL = "DELETE FROM book_type WHERE id = ?";

        try {
            springDB.getJdbcTemplate().update(deleteViewSQL, id);
            int rowsAffected = springDB.getJdbcTemplate().update(deleteTagSQL, id);

            if (rowsAffected > 0) {
                response.put("status", "success");
                response.put("message", "标签删除成功");
            } else {
                response.put("status", "error");
                response.put("message", "未找到对应ID的标签");
            }
        } catch (Exception e) {
            response.put("status", "error");
            response.put("message", "标签删除失败: " + e.getMessage());
        }

        return response;
    }

    public Map<String, Object> updateTag(Integer id, String type) {
        Map<String, Object> response = new HashMap<>();
        String sql = "UPDATE book_type SET type = ? WHERE id = ?";

        try {
            int rowsAffected = springDB.getJdbcTemplate().update(sql, type, id);
            if (rowsAffected > 0) {
                response.put("status", "success");
                response.put("message", "标签更新成功");
            } else {
                response.put("status", "error");
                response.put("message", "未找到对应ID的标签");
            }
        } catch (Exception e) {
            response.put("status", "error");
            response.put("message", "标签更新失败: " + e.getMessage());
        }

        return response;
    }
}
